package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yksj.healthtalk.adapter.SearchAdapter.onClickSalonAttentionListener;
import com.yksj.healthtalk.comm.DeleteOrderBtnFragmentDialog;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.home.MySalonListFragment;
import com.yksj.healthtalk.utils.BitmapUtils;

public class MySalonListExpandableAdapter extends BaseExpandableListAdapter {
	private Map<String, ArrayList<GroupInfoEntity>> map;
	private Context mContext;
	private LayoutInflater inflater;
	private String[] arrayName;
	private Bitmap bDown;
	private Bitmap bUp;
	private Bitmap bg;
	private BitmapDrawable bg_draw;
	private ViewHolder holder;
	private ImageLoader mImageLoader;
	private onClickGroupHeadListener clickGroupHead;// 点击头像进入群的详细资料
	private AppData appData;
	private MySalonListFragment f;
	private onClickSalonAttentionListener mAttentionListener;
	public void setonClickSalonAttentionListener(onClickSalonAttentionListener l){
		this.mAttentionListener = l;
	}
	public interface onClickSalonAttentionListener{
		void onSalonAttentionClick(View v,GroupInfoEntity mSalonName,int positon);
	}
	public MySalonListExpandableAdapter(Context mContext,
			MySalonListFragment mySalonListFragment) {
		this.mContext = mContext;
		this.f = mySalonListFragment;
		appData = HTalkApplication.getAppData();
//		arrayName = new String[] { mContext.getString(R.string.my_focus_salon),
//				mContext.getString(R.string.my_create_salon),
//				mContext.getString(R.string.myBoughtGroup), "购买失败的话题", "购买过的话题" };
		arrayName = new String[] { mContext.getString(R.string.my_focus_salon),
				mContext.getString(R.string.my_create_salon),
				mContext.getString(R.string.myBoughtGroup), "更多..."};
		Resources resources = mContext.getResources();
		this.inflater = LayoutInflater.from(mContext);
		mImageLoader = ImageLoader.getInstance();
		bDown = BitmapUtils.getBackGroundBitmap(resources,R.drawable.friend_group_arrows_down);
		bUp = BitmapUtils.getBackGroundBitmap(resources,R.drawable.friend_group_arrows);
		bg = BitmapUtils.getBackGroundBitmap(resources,R.drawable.setting_title_bg);
		bg_draw = new BitmapDrawable(bg);
	}

	@Override
	public int getGroupCount() {
		return 4;
	}
   
	@Override
	public int getChildrenCount(int groupPosition) {
		switch (groupPosition) {
		case 0:
			return appData.getInterestGroupIdList().size();
		case 1:
			return appData.getCreatedGroupIdList().size();
		case 2:
			return appData.getMyBoughtGroupIdList().size();
		case 3:
			return 0;
//		case 3:
//			return appData.getbuyFailureGroupIdList().size();
//		case 4:
//			return appData.getMyBoughtHisGroupIdList().size();
		}
		return groupPosition;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null || convertView.getTag() == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.knowledge_detail_expand_group, null);
			holder.mGroupName = (TextView) convertView.findViewById(R.id.expand_group_text);
			holder.mGroupNumber = (TextView) convertView.findViewById(R.id.group_number);
			holder.mImageIcon = (ImageView) convertView.findViewById(R.id.expand_group_head);
			holder.mLayout = (LinearLayout) convertView.findViewById(R.id.group_number_lay);
			convertView.setTag(holder);
		}
		holder = (ViewHolder) convertView.getTag();
		holder.mGroupName.setText(arrayName[groupPosition]);
		if(groupPosition!=3)
		holder.mGroupNumber.setText("("+String.valueOf(getChildrenCount(groupPosition))+")");
		holder.mLayout.setVisibility(View.VISIBLE);
		convertView.setBackgroundDrawable(bg_draw);
		if (!isExpanded) {
			holder.mImageIcon.setImageBitmap(bUp);
		} else {
			holder.mImageIcon.setImageBitmap(bDown);
		}
		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		final GroupInfoEntity mInfoEntity;
		switch (groupPosition) {
		case 0:
			mInfoEntity = (GroupInfoEntity) appData.getGroupInfoEntity(appData
					.getInterestGroupIdList().get(childPosition));
			break;
		case 1:
			mInfoEntity = (GroupInfoEntity) appData.getGroupInfoEntity(appData
					.getCreatedGroupIdList().get(childPosition));
			break;
		case 2:
			mInfoEntity = (GroupInfoEntity) appData.getGroupInfoEntity(appData
					.getMyBoughtGroupIdList().get(childPosition));
			break;
//		case 3:
//			mInfoEntity = (GroupInfoEntity) appData.getGroupInfoEntity(appData
//					.getbuyFailureGroupIdList().get(childPosition));
//			break;
//		case 4:
//			mInfoEntity = (GroupInfoEntity) appData.getGroupInfoEntity(appData
//					.getMyBoughtHisGroupIdList().get(childPosition));
//			break;
		default:
			mInfoEntity = new GroupInfoEntity();
			break;
		}
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.my_salon_item, null);
			holder.image = (ImageView) convertView.findViewById(R.id.head_image);
			holder.salonLevel = (ImageView) convertView.findViewById(R.id.levl);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name);
			holder.note_tv = (TextView) convertView.findViewById(R.id.note);
			holder.numMsg = (TextView) convertView.findViewById(R.id.item_dele);
			holder.collectFees = (ImageView) convertView.findViewById(R.id.money);
			holder.doctorSalon = (ImageView) convertView.findViewById(R.id.doctor_salon);
			holder.itemLine = (ImageView) convertView.findViewById(R.id.item_line);
			holder.attention = (ImageView) convertView.findViewById(R.id.attention);
			holder.payBtn = (Button) convertView.findViewById(R.id.give_money);
			holder.deleteBtn = (Button) convertView.findViewById(R.id.delete);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 是否有分割线
		if (isLastChild) {
			holder.itemLine.setVisibility(View.GONE);
		} else {
			holder.itemLine.setVisibility(View.VISIBLE);
		}
		// 消息
		holder.image.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (clickGroupHead != null) {
					clickGroupHead.onClickFriendHead(mInfoEntity);
				}

			}
		});
		// 未读消息
		holder.numMsg.setVisibility(View.VISIBLE);
		int size = appData.getNoReadMesgSize(mInfoEntity.getId());
		if (appData.getNoReadMesgSize(mInfoEntity.getId()) != 0) {
			String readCount=size >100 ? "99+":String.valueOf(size);
			holder.numMsg.setText(readCount);
		} else {
			holder.numMsg.setVisibility(View.GONE);
		}
		
		// 是否收费
		if (mInfoEntity.isCharge()) {//(显示支付和删除按钮)
			holder.collectFees.setVisibility(View.VISIBLE);
			if (!TextUtils.isEmpty(mInfoEntity.getOrderInfo())) {
				try {
					JSONObject object = new JSONObject(
							mInfoEntity.getOrderInfo());
					// 显示在我购买的话题里面
					if (object.has("ORDER_ID") && groupPosition == 2) {
						holder.deleteBtn.setTag(object.get("ORDER_ID"));
						holder.payBtn.setVisibility(View.VISIBLE);
						holder.payBtn.setTag(object.get("PAY_ID"));
						holder.deleteBtn.setVisibility(View.VISIBLE);
					} else {
						holder.payBtn.setVisibility(View.GONE);
						holder.deleteBtn.setVisibility(View.GONE);
					}
				} catch (JSONException e) {
					holder.payBtn.setVisibility(View.GONE);
					holder.deleteBtn.setVisibility(View.GONE);
				}
			} else {
				holder.payBtn.setVisibility(View.GONE);
				holder.deleteBtn.setVisibility(View.GONE);
			}
			holder.attention.setVisibility(View.GONE);
		} else {
			holder.collectFees.setVisibility(View.GONE);
			holder.payBtn.setVisibility(View.GONE);
			holder.deleteBtn.setVisibility(View.GONE);
		}
		//只能显示一个
		if(holder.payBtn.getVisibility()==8&&holder.deleteBtn.getVisibility()==8){
			holder.attention.setVisibility(View.VISIBLE);
		}else{
			holder.attention.setVisibility(View.GONE);
		}
		
		// 支付
		holder.payBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (f instanceof OnPayClickListener) {
					Object object = v.getTag();
					if (object != null) {
						f.OnPayClick(String.valueOf(object));
					}
				}
			}
		});
		// 删除
		holder.deleteBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (f instanceof OnClickDeleteListener) {
					if (v.getTag() != null ) {
//						showuploadPopWindow(mInfoEntity , );
						DeleteOrderBtnFragmentDialog.show(f.getFragmentManager(),String.valueOf(v.getTag()), f,null);
					}
				}

			}
		});
		// salon级别
		holder.salonLevel.getDrawable().setLevel(
				Integer.valueOf(mInfoEntity.getGroupLevel()));
		// 判断是否是医师话题
		if (!mInfoEntity.isSalon()) {
			holder.doctorSalon.setVisibility(View.GONE);
		} else {
			holder.doctorSalon.setVisibility(View.VISIBLE);
		}
		//关注状态
		if (mInfoEntity.isSalonAttention()) {
			holder.attention.setImageResource(R.drawable.not_attention);
		}else {
			holder.attention.setImageResource(R.drawable.attention);
		}
		
		//关注回调
		holder.attention.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mAttentionListener != null) {
					mAttentionListener.onSalonAttentionClick(holder.attention, mInfoEntity, childPosition);
				}
			}
		});
		
		mImageLoader.displayImage("4",mInfoEntity.getNormalHeadIcon(),holder.image);
		holder.name_tv.setText(mInfoEntity.getName());
		holder.note_tv.setText(mInfoEntity.getRecordDesc());
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public void setIntoInfoListener(MySalonListFragment mySalonListFragment) {
	}

	// 支付按钮
	public interface OnPayClickListener {
		void OnPayClick(String payId);
	}

	// 删除订单
	public interface OnClickDeleteListener {
		void OnClickDelete(GroupInfoEntity entity, String orderId);
	}

	public interface onClickGroupHeadListener {
		void onClickFriendHead(GroupInfoEntity entity);
	}

	public void setonClickGroupHeadListener(
			onClickGroupHeadListener groupHeadListener) {
		this.clickGroupHead = groupHeadListener;
	}

	private static class ViewHolder {
		private ImageView attention;
		private ImageView mImageIcon;
		private TextView mGroupNumber;
		private TextView mGroupName;
		private LinearLayout mLayout;
		private TextView numMsg;
		private ImageView collectFees;// 是否收费
		private ImageView image; // 头像
		private TextView name_tv; // 名称
		private TextView note_tv; // 简介
		ImageView salonLevel; // 等级
		ImageView doctorSalon;// 医师话题
		private ImageView itemLine;// 分割线
		Button payBtn;
		Button deleteBtn;
	}
}
